package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.ShopRoleId;
import com.moyq5.permit.center.bean.entity.Shop;
import com.moyq5.permit.center.bean.entity.ShopRole;
import com.moyq5.permit.center.bean.search.ShopRole4Search;
import com.moyq5.permit.center.feign.client.ShopRoleFeign;
import com.moyq5.permit.center.web.jackson.filter.ShopRoleFilter;

/**
 * 门店角色
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/shopRole")
public class ShopRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(ShopRoleController.class);
	
	@Autowired
	private ShopRoleFeign shopRoleFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/role", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithRole(ShopRole4Search shopRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询门店角色信息>>>pageWithRole(ShopRole4Search, PageData)");
		if (null != getLoginShopId()) {
			if (null == shopRole4s.getShop()) {
				shopRole4s.setShop(new Shop());
			}
			shopRole4s.getShop().setShopId(getLoginShopId());;
		}
		return toJson(shopRoleFeign.pageWithRole(new Body<ShopRole4Search, PageData>(shopRole4s, pd)), ShopRoleFilter.getWithRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/shopAndrole", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithShopAndRole(ShopRole4Search shopRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询门店角色信息>>>pageWithShopAndRole(ShopRole4Search, PageData)");
		if (null != getLoginShopId()) {
			if (null == shopRole4s.getShop()) {
				shopRole4s.setShop(new Shop());
			}
			shopRole4s.getShop().setShopId(getLoginShopId());;
		}
		return toJson(shopRoleFeign.pageWithShopAndRole(new Body<ShopRole4Search, PageData>(shopRole4s, pd)), ShopRoleFilter.getWithShopAndRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopRole
	 * @return
	 */
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<ShopRoleId> add(@RequestBody ShopRole shopRole) {
		log.debug("添加门店角色信息>>>add(ShopRole)");
		if (null != getLoginShopId()) {
			shopRole.getId().setShopId(getLoginShopId());
		}
		ObjectResult<ShopRole> objRst = shopRoleFeign.one(shopRole.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<ShopRoleId> idRst = new ObjectResult<ShopRoleId>();
			idRst.setOk();
			idRst.setData(shopRole.getId());
			return idRst;
		}
		return shopRoleFeign.create(shopRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopRole
	 * @return
	 */
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated ShopRole shopRole) {
		log.debug("更新门店角色信息>>>edit(ShopRole)");
		if (null != getLoginShopId()) {
			shopRole.getId().setShopId(getLoginShopId());
		}
		return shopRoleFeign.update(shopRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopRoleId
	 * @return
	 */
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody ShopRoleId shopRoleId) {
		log.debug("删除门店角色信息>>>del(ShopRoleId)");
		if (null != getLoginShopId()) {
			shopRoleId.setShopId(getLoginShopId());
		}
		return shopRoleFeign.remove(shopRoleId);
	}
	
}
